package httpd

import (
	"devops-tools/server"
	"encoding/json"
	"fmt"
	"github.com/sirupsen/logrus"
	"math"
	"net/http"
	"strconv"
)

//检查服务
func CheckServerStatus(w http.ResponseWriter, r *http.Request) {
	var result = Success()
	port := r.FormValue("port")
	if port == "" {
		result = Fail("请输入端口号")
	} else {

	}
	bytes, _ := json.Marshal(result)
	fmt.Fprint(w, string(bytes))
}

//启动服务
func StartServer(w http.ResponseWriter, r *http.Request) {
	logrus.Print("启动服务")
	var result = Success()
	port := r.FormValue("port")
	if port == "" {
		result = Fail("请输入端口号")
	} else {
		num, e := strconv.Atoi(port)
		if e != nil {
			result = Fail("端口号有误")
		} else {
			num = int(math.Sqrt(float64(num)))
			logrus.Print("端口号:", num)
			server.StartServer(num)
		}
	}
	bytes, _ := json.Marshal(result)
	fmt.Fprint(w, string(bytes))
}

//监控数据
func MonitorData(w http.ResponseWriter, r *http.Request) {
	logrus.Println("查询可用内存")
	memory := server.GetFreeMemory()
	var result = OK(memory)
	bytes, _ := json.Marshal(result)
	fmt.Fprint(w, string(bytes))
}
